package biz.bbtec.sbm.store.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import biz.bbtec.sbm.store.bean.AddressBean;
import biz.bbtec.sbm.store.bean.CheckBean;
import biz.bbtec.sbm.store.bean.ItemBean;
import biz.bbtec.sbm.store.bean.StoreBean;
import biz.bbtec.sbm.store.factory.StoreFactory;
import biz.bbtec.sbm.store.service.StoreService;
import biz.bbtec.sbm.user.bean.UserBean;

public class StoreAction extends ActionSupport implements SessionAware {

	private ItemBean item;
	private AddressBean add;
	private List<StoreBean> storelist = new ArrayList<StoreBean>();
	private CheckBean check;
	private StoreBean store;
	private List<AddressBean> addlist = new ArrayList<AddressBean>();
	private int adsid;
	private Map session;

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public int getAdsid() {
		return adsid;
	}

	public void setAdsid(int adsid) {
		this.adsid = adsid;
	}

	public List<AddressBean> getAddlist() {
		return addlist;
	}

	public void setAddlist(List<AddressBean> addlist) {
		this.addlist = addlist;
	}

	public StoreBean getStore() {
		return store;
	}

	public void setStore(StoreBean store) {
		this.store = store;
	}

	public AddressBean getAdd() {
		return add;
	}

	public void setAdd(AddressBean add) {
		this.add = add;
	}

	public CheckBean getCheck() {
		return check;
	}

	public void setCheck(CheckBean check) {
		this.check = check;
	}

	public ItemBean getItem() {
		return item;
	}

	public void setItem(ItemBean item) {
		this.item = item;
	}

	public List<StoreBean> getStorelist() {
		return storelist;
	}

	public void setStorelist(List<StoreBean> storelist) {
		this.storelist = storelist;
	}

	public String showallstore() {
		StoreService storeservice = StoreFactory.getInstance()
				.creattemService();
		if(item == null){
			item = new ItemBean();
			Map session = ActionContext.getContext().getSession();
			store = (StoreBean) session.get("modifystore");
			item.setItemid(store.getItemid());
			session.remove("modifystore");
		}
		storelist = storeservice.ShowAllStore(item);
		if (storelist.size() == 0) {
			this.addActionError("该货物未入库！");
			return INPUT;
		}
		return SUCCESS;
	}

	public String showaddstore() {
		StoreService storeservice = StoreFactory.getInstance()
				.creattemService();
		storelist = storeservice.ShowAddAtore(add);
		if (storelist.size() == 0) {
			this.addActionError("该仓库没有储存货物！");
			return INPUT;
		}
		return SUCCESS;
	}

	public String ShowOneStore() {
		StoreService storeservice = StoreFactory.getInstance()
				.creattemService();
		if(store.getStoreid() == 0){
			Map session = ActionContext.getContext().getSession();
			store = new StoreBean();
			store = (StoreBean) session.get("modifystore");
			session.remove("modifystore");
		}
		store = storeservice.showonestore(store.getStoreid());
		if (store == null) {
			this.addActionError("出错啦！");
			return INPUT;
		}
		Map session = ActionContext.getContext().getSession();
		UserBean user = (UserBean) session.get("user");
		addlist = storeservice.showalladdinfo(user, store.getAdsid());
		if (addlist.size() == 0) {
			this.addActionError("您所在公司没有别的存储地址，请添加！");
			return INPUT;
		}
		return SUCCESS;
	}

	public String ModifyStore() {
		StoreService storeservice = StoreFactory.getInstance()
				.creattemService();
		if (!(storeservice.modifystore(store, getAdsid()))) {
			this.addActionError("数据库操作失败！");
			return INPUT;
		}
		this.session.put("modifystore", store);
		return SUCCESS;
	}
}
